package com.xinruke.hostels.systemmanage.service.Impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xinruke.hostels.cache.extract.extractor.DictInfoCacheExtractor;
import com.xinruke.hostels.common.cache.ManageUserCache;
import com.xinruke.hostels.common.cache.vo.LoginManageResultVO;
import com.xinruke.hostels.common.constants.SysDicConstant;
import com.xinruke.hostels.common.mybatis.MyExample;
import com.xinruke.hostels.common.po.YwCanting;
import com.xinruke.hostels.common.vo.ResultVO;
import com.xinruke.hostels.common.vo.query.EConditionOperator;
import com.xinruke.hostels.common.vo.query.QueryConditions;
import com.xinruke.hostels.common.vo.query.RowsDataVO;
import com.xinruke.hostels.system.manage.interfaces.dto.*;
import com.xinruke.hostels.systemmanage.mapper.YwCantingMapper;
import com.xinruke.hostels.systemmanage.service.RestaurantService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 餐厅维护
 *
 * @author Jinz
 * @date 2019-06-27 1:25
 */
@Service
@Slf4j
public class RestaurantServiceImpl implements RestaurantService {

    @Autowired
    private YwCantingMapper ywCantingMapper;

    @Autowired
    private DictInfoCacheExtractor dictInfoCacheExtractor;

    @Autowired
    private ManageUserCache manageUserCache;

    @Override
    public ResultVO add(RestaurantAddDTO params) {
        ResultVO resultVO;
        YwCanting ywCanting = new YwCanting();
        BeanUtils.copyProperties(params, ywCanting);

        LoginManageResultVO userInfo = manageUserCache.getLoginManageUserInfo();

        ywCanting.setJiudianId(userInfo.getJiudianId());
        ywCanting.setChuangjianrenId(userInfo.getId());
        ywCanting.setChuangjianshijian(new Date());
        ywCanting.setGengxinrenId(userInfo.getId());
        ywCanting.setGengxinshijian(new Date());
        ywCanting.setZhuangtai(SysDicConstant.ZHUANGTAI_YOU_XIAO);
        ywCanting.setShanchubiaoshi(SysDicConstant.DELFLAG_0);

        try {
            int check = ywCantingMapper.insert(ywCanting);
            if (check > 0) {
                resultVO = ResultVO.success();
            } else {
                resultVO = ResultVO.fail();
            }
        } catch (Exception e) {
            resultVO = ResultVO.fail();
            log.error("餐厅新增异常，餐厅信息为={}", JSONObject.toJSON(ywCanting), e);
        }
        return resultVO;
    }

    @Override
    public ResultVO modify(RestaurantUpdateDTO params) {
        ResultVO resultVO;
        YwCanting ywCanting = new YwCanting();
        BeanUtils.copyProperties(params, ywCanting);
        ywCanting.setGengxinrenId(manageUserCache.getCurrentUserId());
        ywCanting.setGengxinshijian(new Date());

        try {
            int check = ywCantingMapper.updateByPrimaryKeySelective(ywCanting);
            if (check > 0) {
                resultVO = ResultVO.success();
            } else {
                resultVO = ResultVO.fail();
            }
        } catch (Exception e) {
            resultVO = ResultVO.fail();
            log.error("餐厅修改异常，餐厅信息为={}", JSONObject.toJSON(ywCanting), e);
        }
        return resultVO;
    }

    @Override
    public ResultVO delete(RestaurantDelDTO restaurantDelQueryDTO) {
        ResultVO resultVO;
        YwCanting ywCanting = new YwCanting();
        ywCanting.setId(restaurantDelQueryDTO.getId());
        ywCanting.setShanchubiaoshi(SysDicConstant.DELFLAG_1);
        ywCanting.setGengxinrenId(manageUserCache.getCurrentUserId());
        ywCanting.setGengxinshijian(new Date());

        try {
            int check = ywCantingMapper.updateByPrimaryKeySelective(ywCanting);
            if (check > 0) {
                resultVO = ResultVO.success();
            } else {
                resultVO = ResultVO.fail();
            }
        } catch (Exception e) {
            resultVO = ResultVO.fail();
            log.error("餐厅删除异常，餐厅信息为={}", JSONObject.toJSON(ywCanting), e);
        }
        return resultVO;
    }

    @Override
    public ResultVO<RestaurantDetailQueryResultDTO> detail(RestaurantDetailQueryDTO restaurantDetailQueryDTO) {
        ResultVO<RestaurantDetailQueryResultDTO> resultVO = ResultVO.success();
        YwCanting param = new YwCanting();
        param.setId(restaurantDetailQueryDTO.getId());
        YwCanting ywCanting = ywCantingMapper.selectByPrimaryKey(param);
        RestaurantDetailQueryResultDTO dto = new RestaurantDetailQueryResultDTO();
        BeanUtils.copyProperties(ywCanting, dto);
        dto.setZhuangtaiMingCheng(dictInfoCacheExtractor.getDictValue(SysDicConstant.ZHUANGTAI, dto.getZhuangtai()));
        dto.setCanzhuoleixingMingCheng(dictInfoCacheExtractor.getDictValue(SysDicConstant.CANZHUOLEIXING, dto.getCanzhuoleixing()));
        resultVO.setData(dto);
        return resultVO;
    }

    @Override
    public ResultVO<RowsDataVO<RestaurantDetailQueryResultDTO>> restaurants(RestaurantsQueryDTO params) {
        RowsDataVO<RestaurantDetailQueryResultDTO> dataVO = new RowsDataVO<>();
        QueryConditions conditions = new QueryConditions();
        conditions.addCondition("shanchubiaoshi", EConditionOperator.EQULS, SysDicConstant.DELFLAG_0);
        conditions.addCondition("jiudianId", EConditionOperator.EQULS, manageUserCache.getJiudianId());
        conditions.addCondition("canzhuoleixing", EConditionOperator.EQULS, params.getCanzhuoleixing());
        conditions.addCondition("mingcheng", EConditionOperator.LIKE, params.getMingcheng());

        MyExample example = new MyExample(YwCanting.class, conditions, params.getOrderFields());
        Page<RestaurantDetailQueryResultDTO> page = PageHelper.startPage(params.getPageNo(), params.getPageSize());
        List<YwCanting> ywCantings = ywCantingMapper.selectByExample(example);
        List<RestaurantDetailQueryResultDTO> resultList = ywCantings.stream().map(n -> {
            RestaurantDetailQueryResultDTO dto = new RestaurantDetailQueryResultDTO();
            BeanUtils.copyProperties(n, dto);
            dto.setZhuangtaiMingCheng(dictInfoCacheExtractor.getDictValue(SysDicConstant.ZHUANGTAI, dto.getZhuangtai()));
            dto.setCanzhuoleixingMingCheng(dictInfoCacheExtractor.getDictValue(SysDicConstant.CANZHUOLEIXING, dto.getCanzhuoleixing()));
            return dto;
        }).collect(Collectors.toList());
        dataVO.setPageNo(page.getPageNum());
        dataVO.setTotalRecords(page.getTotal());
        dataVO.setTotalPages(page.getPages());
        dataVO.setRows(resultList);
        ResultVO<RowsDataVO<RestaurantDetailQueryResultDTO>> result = ResultVO.success();
        result.setData(dataVO);
        return result;
    }

    @Override
    public ResultVO<RowsDataVO<ReservationRestaurantQueryResultDTO>> reservationRestaurants(ReservationRestaurantQueryDTO params) {
        RowsDataVO<ReservationRestaurantQueryResultDTO> dataVO = new RowsDataVO<>();
        QueryConditions conditions = new QueryConditions();
        conditions.addCondition("shanchubiaoshi", EConditionOperator.EQULS, SysDicConstant.DELFLAG_0);
        conditions.addCondition("jiudianId", EConditionOperator.EQULS, params.getJiudianId());
        conditions.addCondition("canzhuoleixing", EConditionOperator.EQULS, params.getCanzhuoleixing());
        conditions.addCondition("mingcheng", EConditionOperator.LIKE, params.getMingcheng());

        MyExample example = new MyExample(YwCanting.class, conditions, params.getOrderFields());
        Page<RestaurantDetailQueryResultDTO> page = PageHelper.startPage(params.getPageNo(), params.getPageSize());
        List<YwCanting> ywCantings = ywCantingMapper.selectByExample(example);
        List<ReservationRestaurantQueryResultDTO> resultList = ywCantings.stream().map(n -> {
            ReservationRestaurantQueryResultDTO dto = new ReservationRestaurantQueryResultDTO();
            BeanUtils.copyProperties(n, dto);
            dto.setZhuangtaiMingCheng(dictInfoCacheExtractor.getDictValue(SysDicConstant.ZHUANGTAI, dto.getZhuangtai()));
            dto.setCanzhuoleixingMingCheng(dictInfoCacheExtractor.getDictValue(SysDicConstant.CANZHUOLEIXING, dto.getCanzhuoleixing()));
            return dto;
        }).collect(Collectors.toList());
        dataVO.setPageNo(page.getPageNum());
        dataVO.setTotalRecords(page.getTotal());
        dataVO.setTotalPages(page.getPages());
        dataVO.setRows(resultList);
        ResultVO<RowsDataVO<ReservationRestaurantQueryResultDTO>> result = ResultVO.success();
        result.setData(dataVO);
        return result;
    }

    @Override
    public RowsDataVO<RestaurantUnusedQueryResultDTO> getRestaurantUnusedList(RestaurantUnusedQueryDTO entity) {
        RowsDataVO<RestaurantUnusedQueryResultDTO> rowsDataVO = new RowsDataVO<RestaurantUnusedQueryResultDTO>();

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("jiudianid", manageUserCache.getJiudianId());
        paramMap.put("kaishishijian", entity.getKaishishijian());
        paramMap.put("jieshushijian", entity.getJieshushijian());
        if (entity.getCanzhuoleixing() != null) {
            paramMap.put("canzhuoleixing", entity.getCanzhuoleixing());
        }
        if (entity.getZhuangtai() != null) {
            paramMap.put("zhuangtai", entity.getZhuangtai());
        }

        Page<RestaurantUnusedQueryResultDTO> page = PageHelper.startPage(entity.getPageNo(), entity.getPageSize());
        List<RestaurantUnusedQueryResultDTO> unusedQueryResultDTOList = ywCantingMapper.getRestaurantUnusedList(paramMap);
        for (RestaurantUnusedQueryResultDTO restaurantUnusedQueryResultDTO : unusedQueryResultDTOList) {
            restaurantUnusedQueryResultDTO.setJiucanleibie(entity.getJiucanleibie());
        }
        rowsDataVO.setRows(unusedQueryResultDTOList);
        rowsDataVO.setPageNo(page.getPageNum());
        rowsDataVO.setTotalPages(page.getTotal());
        rowsDataVO.setTotalRecords(page.getPages());
        return rowsDataVO;
    }
}
